using System.Collections.Generic;
using System.IO;
using System.Linq;
using Common;
using Common.NotifyEvent;

namespace SSettings
{
    public class ProjectReportSetting : NotifyPropertyChanged
    {
        private string _HospitalName;

        private List<KeyValuePair> _ProjectReportList = new List<KeyValuePair>();

        public string HospitalName
        {
            get
            {
                return _HospitalName;
            }
            set
            {
                if (!(_HospitalName == value))
                {
                    _HospitalName = value;
                    OnPropertyChanged("HospitalName");
                }
            }
        }

        public List<KeyValuePair> ProjectReportList
        {
            get
            {
                return _ProjectReportList;
            }
            set
            {
                if (_ProjectReportList != value)
                {
                    _ProjectReportList = value;
                    OnPropertyChanged("ProjectReportList");
                }
            }
        }

        public ProjectReportSetting Clone()
        {
            ProjectReportSetting projectReportSetting = new ProjectReportSetting();
            StreamEx streamEx = new StreamEx();
            SaveToStream(streamEx);
            streamEx.Seek(0L, SeekOrigin.Begin);
            projectReportSetting.ReadFromStream(streamEx);
            return projectReportSetting;
        }

        public void SetProjectReport(string prjName, string report)
        {
            if (!string.IsNullOrEmpty(prjName))
            {
                if (report == null)
                {
                    report = string.Empty;
                }
                KeyValuePair keyValuePair = ProjectReportList.Where((KeyValuePair s) => s.Key.ToString() == prjName).FirstOrDefault();
                if (keyValuePair != null)
                {
                    keyValuePair.Value = report;
                    return;
                }
                ProjectReportList.Add(new KeyValuePair
                {
                    Key = prjName,
                    Value = report
                });
            }
        }

        public string GetProjectReport(string prjName)
        {
            KeyValuePair keyValuePair = ProjectReportList.Where((KeyValuePair s) => s.Key.ToString() == prjName).FirstOrDefault();
            if (keyValuePair != null)
            {
                return keyValuePair.Value as string;
            }
            return null;
        }

        public void ReomoveProjectReport(string prjName)
        {
            KeyValuePair keyValuePair = ProjectReportList.Where((KeyValuePair s) => s.Key.ToString() == prjName).FirstOrDefault();
            if (keyValuePair != null)
            {
                ProjectReportList.Remove(keyValuePair);
            }
        }

        public void SaveToStream(StreamEx stream)
        {
            stream.Write(PropertyKey.Key_ProjectReportSetting);
            List<PropertyKey> list = new List<PropertyKey>();
            List<byte[]> list2 = new List<byte[]>();
            list.Add(PropertyKey.Key_ProjectReportList);
            StreamEx streamEx = new StreamEx();
            streamEx.Write(ProjectReportList.Count);
            foreach (KeyValuePair projectReport in ProjectReportList)
            {
                streamEx.Write(projectReport.Key.ToString());
                streamEx.Write(projectReport.Value.ToString());
            }
            streamEx.Seek(0L, SeekOrigin.Begin);
            list2.Add(streamEx.ToArray());
            streamEx.Close();
            if (!string.IsNullOrEmpty(HospitalName))
            {
                list.Add(PropertyKey.Key_HospitalName);
                list2.Add(BytesConverter.GetBytes(HospitalName));
            }
            stream.Write(PropertyKey.Key_Index);
            stream.WriteIndex(list, list2);
            stream.Flush();
        }

        public bool ReadFromStream(StreamEx stream)
        {
            PropertyKey propertyKey = stream.ReadKey();
            if (propertyKey != PropertyKey.Key_ProjectReportSetting)
            {
                stream.Seek(-4L, SeekOrigin.Current);
                return false;
            }
            propertyKey = stream.ReadKey();
            if (propertyKey == PropertyKey.Key_Index)
            {
                List<PropertyKey> keys;
                List<byte[]> dates;
                stream.ReadIndex(out keys, out dates);
                for (int i = 0; i < keys.Count; i++)
                {
                    switch (keys[i])
                    {
                        case PropertyKey.Key_ProjectReportList:
                            {
                                StreamEx streamEx = new StreamEx();
                                streamEx.Write(dates[i]);
                                streamEx.Seek(0L, SeekOrigin.Begin);
                                int num = streamEx.ReadInt();
                                for (int j = 0; j < num; j++)
                                {
                                    string prjName = streamEx.ReadString();
                                    string value = streamEx.ReadString();
                                    KeyValuePair keyValuePair = ProjectReportList.Where((KeyValuePair s) => s.Key.ToString() == prjName).FirstOrDefault();
                                    if (keyValuePair == null)
                                    {
                                        ProjectReportList.Add(new KeyValuePair
                                        {
                                            Key = prjName,
                                            Value = value
                                        });
                                    }
                                }
                                streamEx.Close();
                                break;
                            }
                        case PropertyKey.Key_HospitalName:
                            HospitalName = BytesConverter.BytesToString(dates[i]);
                            break;
                    }
                }
            }
            else
            {
                stream.Seek(-4L, SeekOrigin.Current);
                int num2 = stream.ReadInt();
                for (int k = 0; k < num2; k++)
                {
                    string prjName2 = stream.ReadString();
                    string value2 = stream.ReadString();
                    KeyValuePair keyValuePair2 = ProjectReportList.Where((KeyValuePair s) => s.Key.ToString() == prjName2).FirstOrDefault();
                    if (keyValuePair2 == null)
                    {
                        ProjectReportList.Add(new KeyValuePair
                        {
                            Key = prjName2,
                            Value = value2
                        });
                    }
                }
            }
            return true;
        }
    }
}
